package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.SelectMenu;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private SelectMenu selectMenu;

    @Override
    public User findByUsername(String username) {
        //先查基本信息
        User user = userDao.selectByUsername(username);
        if (null == user) {
            //用户不存在
            return null;
        }
        //根据用户ID查角色信息
        List<Role> roles = roleDao.selectByUserId(user.getId());
        for (Role role : roles) {
            //根据角色ID查权限信息
            List<Permission> permissions = permissionDao.selectByRoleId(role.getId());
            role.getPermissions().addAll(permissions);
        }
        user.getRoles().addAll(roles);
        return user;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}",queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询page
        Page<User> page= userDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 添加用户
     * @param user
     * @param roleIds
     */
    @Override
    public void add(User user, Integer[] roleIds) {
     userDao.insert(user);
        for (Integer roleId : roleIds) {
            userDao.insertUserAndRole(user.getId(),roleId);
        }
    }

    /**
     * 编辑用户
     * @param user
     * @param roleIds
     */
    @Override
    public void edit(User user, Integer[] roleIds) {
        //1、更新用户
        userDao.update(user);
        //2、更新关联关系
        //2.1、删除原有关系
        userDao.deleteUserAndroleByUserId(user.getId());
        //2.2、加入当前关联关系
        for (Integer roleId : roleIds) {
            userDao.insertUserAndRole(user.getId(),roleId);
    }
}

    @Override
    public void delete(Integer id) {
        userDao.deleteUserAndroleByUserId(id);
        userDao.delete(id);
    }

    @Override
    public User findById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    public List<Integer> findroleIdsByuserId(Integer id) {
        return userDao.findroleIdsByuserId(id);
    }

    @Override
    public List<Menu> selectById(Integer id) {
        return selectMenu.selectById(id);
    }



    @Override
    public int selectByUserName(String username) {
        return selectMenu.selectByUserName(username);
    }

    @Override
    public List<Menu> selectMenuIdAndParentId(Integer id) {
        return selectMenu.selectByLevel(id);
    }

    @Override
    public List<Menu> selectLevl() {
        return selectMenu.selectLevl();
    }

    @Override
    public List<Menu> selectByMenuid(Integer parentMenuId) {
        return selectMenu.selectByMenuid(parentMenuId);
    }

    @Override
    public Menu selectPriority(String username) {
        return selectMenu.selectPriority(username);
    }
}
